<?php

namespace app\model;

use support\Db;
use support\Model;

class WaMembers
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_members';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * 客户列表 - 分页
     * @return false|string
     */
    public static function list($page, $pageNum,$whereIn, $fields, $condition, $between)
    {
        $offset = ($page - 1) * $pageNum;
        if ($between) {
            if ($whereIn){
                $model = Db::table('wa_members')
                    ->select($fields)
                    ->where($condition)
                    ->where('status', '!=', 4)
                    ->whereIn('created_id',$whereIn)
                    ->whereBetween('maintainer_admin_id', $between);
            }else{
                $model = Db::table('wa_members')
                    ->select($fields)
                    ->where($condition)
                    ->where('status', '!=', 4)
                    ->whereBetween('created_at', $between);
            }

        } else {
            if ($whereIn){
                $model = Db::table('wa_members')
                    ->select($fields)
                    ->where('status', '!=', 4)
                    ->where($condition)
                    ->whereIn('maintainer_admin_id',$whereIn);
            }else{
                $model = Db::table('wa_members')
                    ->select($fields)
                    ->where('status', '!=', 4)
                    ->where($condition);
            }


        }

        $count = $model->where($condition)->count();
        $list = $model
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();
        foreach ($list as $k) {
            //,维护人(admin_id)
            if ($k->maintainer_admin_id) {
                $admins = WaAdmins::find(['id' => intval($k->maintainer_admin_id)]);
                $admins = json_decode($admins);
                if ($admins) {
                    $maintainer_admin_name = $admins->nickname;
                } else {
                    $maintainer_admin_name = '';
                }

            } else {
                $maintainer_admin_name = '';
            }
            // 创建人
            if ($k->created_id) {
                $admins = WaAdmins::find(['id' => intval($k->created_id)]);
                $admins = json_decode($admins);
                if ($admins) {
                    $created_name = $admins->nickname;
                } else {
                    $created_name = '';
                }
            } else {
                $created_name = '';
            }
            // 渠道
            if ($k->channel_id) {
                $channels = WaChannel::find(['id' => intval($k->channel_id)]);
                $channels = json_decode($channels);
                if ($channels) {
                    $channel_name = $channels->name;
                } else {
                    $channel_name = '';
                }
            } else {
                $channel_name = '';
            }
            // 团队
            if ($k->term_id) {
                $terms = WaDepartment::find(['id' => intval($k->term_id)]);
                $terms = json_decode($terms);
                if ($terms) {
                    $term_name = $terms->name;
                } else {
                    $term_name = '';
                }
            } else {
                $term_name = '';
            }

            // 1 个人 2 企业
            if ($k->type == 1) {
                $k->type_name = '个人';
            }elseif ($k->type == 2){
                $k->type_name = '企业';
            }else{
                $k->type_name = '';
            }

            $k->maintainer_admin_name = $maintainer_admin_name;
            $k->created_name = $created_name;
            $k->channel_name = $channel_name;
            $k->term_name = $term_name;
        }

        return json_encode(['list' => $list, 'count' => $count]);
    }


    /**
     *
     * 客户列表 - 不分页
     * @param $fields
     * @param $condition
     * @return false|string
     */
    public static function all($fields, $condition,$whereIn)
    {
        $list = Db::table('wa_members')
            ->select($fields)
            ->where($condition)
            ->where('status', '!=', 4)
            ->whereIn('maintainer_admin_id',$whereIn)
            ->get();
        if ($list){
            foreach ($list as $k) {
                if ($k->attachment){
                    $k->attachment = json_decode($k->attachment);
                }

                //,推荐人(admin_id)
                if ($k->member_reference_id) {
                    $admins = WaAdmins::find(['id' => intval($k->member_reference_id)]);
                    $admins = json_decode($admins);
                    if ($admins) {
                        $member_reference_name = $admins->nickname;
                    } else {
                        $member_reference_name = '';
                    }

                } else {
                    $member_reference_name = '';
                }


                //,维护人(admin_id)
                if ($k->maintainer_admin_id) {
                    $admins = WaAdmins::find(['id' => intval($k->maintainer_admin_id)]);
                    $admins = json_decode($admins);
                    if ($admins) {
                        $maintainer_admin_name = $admins->nickname;
                    } else {
                        $maintainer_admin_name = '';
                    }

                } else {
                    $maintainer_admin_name = '';
                }
                // 创建人
                if ($k->created_id) {
                    $admins = WaAdmins::find(['id' => intval($k->maintainer_admin_id)]);
                    $admins = json_decode($admins);
                    if ($admins) {
                        $created_name = $admins->nickname;
                    } else {
                        $created_name = '';
                    }
                } else {
                    $created_name = '';
                }
                // 渠道
                if ($k->channel_id) {
                    $channels = WaChannel::find(['id' => intval($k->channel_id)]);
                    $channels = json_decode($channels);
                    if ($channels) {
                        $channel_name = $channels->name;
                    } else {
                        $channel_name = '';
                    }
                } else {
                    $channel_name = '';
                }
                // 团队
                if ($k->term_id) {
                    $terms = WaDepartment::find(['id' => intval($k->term_id)]);
                    $terms = json_decode($terms);
                    if ($terms) {
                        $term_name = $terms->name;
                    } else {
                        $term_name = '';
                    }
                } else {
                    $term_name = '';
                }
                $k->member_reference_name = $member_reference_name;
                $k->maintainer_admin_name = $maintainer_admin_name;
                $k->created_name = $created_name;
                $k->channel_name = $channel_name;
                $k->term_name = $term_name;
            }
        }

        return json_encode($list);
    }


    public static function find($condition)
    {
        $res = Db::table('wa_members')
            ->where($condition)
            ->first();
        if ($res){
            foreach (array($res) as $k) {

                if ($k->attachment){
                    $k->attachment = json_decode($k->attachment);
                }

                //,推荐人(admin_id)
                $member_reference_name = '';
                if ($k->member_reference_id) {
                    $admins = WaAdmins::find(['id' => intval($k->member_reference_id)]);
                    $admins = json_decode($admins);
                    if ($admins) {
                        $member_reference_name = $admins->nickname;
                    }
                }


                if ($k->maintainer_admin_ids) {
                    $k->maintainer_admin_ids = json_decode($k->maintainer_admin_ids);// 维护人回显参数（包括部门层级用于下拉赛选）
                }

                //,维护人(admin_id)
                if ($k->maintainer_admin_id) {
                    $admins = WaAdmins::find(['id' => intval($k->maintainer_admin_id)]);
                    $admins = json_decode($admins);
                    if ($admins) {
                        $maintainer_admin_name = $admins->nickname;
                    } else {
                        $maintainer_admin_name = '';
                    }

                } else {
                    $maintainer_admin_name = '';
                }
                // 创建人
                if ($k->created_id) {
                    $admins = WaAdmins::find(['id' => intval($k->maintainer_admin_id)]);
                    $admins = json_decode($admins);
                    if ($admins) {
                        $created_name = $admins->nickname;
                    } else {
                        $created_name = '';
                    }
                } else {
                    $created_name = '';
                }
                // 渠道
                if ($k->channel_id) {
                    $channels = WaChannel::find(['id' => intval($k->channel_id)]);
                    $channels = json_decode($channels);
                    if ($channels) {
                        $channel_name = $channels->name;
                    } else {
                        $channel_name = '';
                    }
                } else {
                    $channel_name = '';
                }
                // 团队/部门
                if ($k->term_id) {
                    $terms = WaDepartment::find(['id' => intval($k->term_id)]);
                    $terms = json_decode($terms);
                    if ($terms) {
                        $term_name = $terms->name;
                    } else {
                        $term_name = '';
                    }
                } else {
                    $term_name = '';
                }
                // 1 个人 2 企业
                if ($k->type == 1) {
                    $k->type_name = '个人';
                }elseif ($k->type == 2){
                    $k->type_name = '企业';
                }else{
                    $k->type_name = '';
                }




                $res->maintainer_admin_name = $maintainer_admin_name;
                $res->created_name = $created_name;
                $res->channel_name = $channel_name;
                $res->term_name = $term_name;
            }
        }

        return json_encode($res);
    }

    public static function created($data)
    {

        return Db::table('wa_members')->insertGetId($data);

    }

    public static function updated($condition, $data)
    {

        $res = Db::table('wa_members')->where($condition)->update($data);
        return json_encode($res);

    }


}